import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:scalable_image/scalable_image.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'dart:io';
import 'package:flutter_downloader/flutter_downloader.dart';

// ignore: must_be_immutable
class ShowBigImage extends StatefulWidget {
  String imageUrl;
  bool loadArtwork = false;

  ShowBigImage(this.imageUrl, this.loadArtwork);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return BigImageWidget(imageUrl, loadArtwork);
  }
}

class BigImageWidget extends State<ShowBigImage> {
  String imageUrl;
  bool loadArtwork = false;

  BigImageWidget(this.imageUrl, this.loadArtwork);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    print(imageUrl);
    return loadArtwork
        ? Stack(
            children: <Widget>[
              GestureDetector(
                child: Container(
                  color: Colors.black,
                  alignment: Alignment.center,
                  child: SingleChildScrollView(
                    child: ScalableImage(
                      imageProvider: FileImage(File(imageUrl), scale: 2.0),
                      dragSpeed: 4.0,
                      maxScale: 16,
                      wrapInAspect: true,
                    ),
                  ),
                ),
                onTap: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          )
        : Stack(
            children: <Widget>[
              GestureDetector(
                child: Container(
                  color: Colors.black,
                  alignment: Alignment.center,
                  child: SingleChildScrollView(
                    child: ScalableImage(
                      imageProvider:
                          CachedNetworkImageProvider(imageUrl, scale: 2.0),
                      dragSpeed: 4.0,
                      maxScale: 16,
                      wrapInAspect: true,
                    ),
                  ),
                ),
                onTap: () {
                  Navigator.of(context).pop();
                },
              ),
              Container(
                alignment: Alignment.bottomCenter,
                child: GestureDetector(
                  child: Container(
                    margin: EdgeInsets.only(bottom: 30),
                    width: 100,
                    height: 30,
                    decoration: BoxDecoration(
                        color: Colors.black54,
                        borderRadius: BorderRadius.all(Radius.circular(5)),
                        border: Border.all(color: Colors.blueAccent, width: 1)),
                    alignment: Alignment.center,
                    child: Text(
                      "加载原图",
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: 15,
                          decoration: TextDecoration.none,
                          fontWeight: FontWeight.normal),
                    ),
                  ),
                  onTap: () {
                    _loadImage();
                  },
                ),
              ),
            ],
          );
  }

  void _loadImage() async {
    await getExternalStorageDirectory().then((dir) async {
      var t = imageUrl.split("/");
      String path = dir.path + "/flutter_bsbdj";
      if (await Directory(path).exists()) {
        print(path);
        _downLoad(path, t[t.length - 1]);
      } else {
        Directory(path).create(recursive: false).then((dir) {
          print(dir.path);
          _downLoad(path, t[t.length - 1]);
        });
      }
    });
  }

  void _downLoad(String path, String fileName) async {
    await FlutterDownloader.enqueue(
            url: imageUrl,
            savedDir: path,
            showNotification: true,
            openFileFromNotification: true)
        .then((taskId) {
      setState(() {
        loadArtwork = true;
        imageUrl = path + "/$fileName";
      });
    });
  }
}
